package com.loubii.sunshineSchool.ui.avtivity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.loubii.sunshineSchool.R;
import com.loubii.sunshineSchool.adapter.SSMsgAdapter;
import com.loubii.sunshineSchool.httpRequest.HttpBin;
import com.loubii.sunshineSchool.pojo.MsgItem;
import com.loubii.sunshineSchool.pojo.SSMsg;

import java.util.ArrayList;
import java.util.List;

import lombok.SneakyThrows;
import retrofit2.Call;
import retrofit2.Response;


public class SSChatActivity extends BaseActivity {


    private List<SSMsg> ssMsgList = new ArrayList<SSMsg>();
    private List<MsgItem> msgItems;

    private EditText inputText;

    private Button send;
    private TextView name;

    private RecyclerView msgRecyclerView;

    private SSMsgAdapter adapter;
    private Thread thread = new Thread(new Runnable() {
        @SneakyThrows
        @Override
        public void run() {
            while (!isStop) {
                Thread.sleep(5000);
                initMsgs();
            }
        }
    });
    private boolean isStop = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat);
        inputText = (EditText) findViewById(R.id.input_text);
        send = (Button) findViewById(R.id.send);
        msgRecyclerView = (RecyclerView) findViewById(R.id.msg_recycler_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        msgRecyclerView.setLayoutManager(layoutManager);
        adapter = new SSMsgAdapter(ssMsgList);
        msgRecyclerView.setAdapter(adapter);
        name = (TextView) findViewById(R.id.tv_title_time);
        name.setText(getIntent().getCharSequenceExtra("name"));
        initMsgs(); // 初始化消息数据
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = inputText.getText().toString();
                inputText.setText(""); // 清空输入框中的内容
                if (!"".equals(content)) {
                    HttpBin.sendChat(SSChatActivity.this, getIntent().getIntExtra("uid", 0), content, new HttpBin.ResponseListener<Boolean>() {
                        @Override
                        public void onResponse(Call<Boolean> call, Response<Boolean> response) {
                            if (response.body()) {
                                SSMsg ssMsg = new SSMsg(content, SSMsg.TYPE_SENT);
                                ssMsgList.add(ssMsg);
                                adapter.notifyItemInserted(ssMsgList.size() - 1); // 当有新消息时，刷新ListView中的显示
                                msgRecyclerView.scrollToPosition(ssMsgList.size() - 1); // 将ListView定位到最后一行
                            }
                        }

                        @Override
                        public void onFailure(Call<Boolean> call, Throwable t) {

                        }
                    });
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        isStop = false;
        thread.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        isStop = true;
    }

    private void initMsgs() {
        HttpBin.receiveChat(this, getIntent().getIntExtra("uid", 0), new HttpBin.ResponseListener<List<MsgItem>>() {
            @Override
            public void onResponse(Call<List<MsgItem>> call, Response<List<MsgItem>> response) {
                ssMsgList.clear();
                msgItems = response.body();
                for (MsgItem item : msgItems) {
                    ssMsgList.add(new SSMsg(item.getMsg(), item.isSend() ? SSMsg.TYPE_SENT : SSMsg.TYPE_RECEIVED));
                }
                adapter.notifyDataSetChanged();
                msgRecyclerView.scrollToPosition(ssMsgList.size() - 1);
            }

            @Override
            public void onFailure(Call<List<MsgItem>> call, Throwable t) {

            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.chat;
    }

    @Override
    protected void initView() {
        initRecyclerView();
    }

    private void initRecyclerView() {

    }


}
